package com.online.questionnaire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.online.questionnaire.common.ErrorCode;
import com.online.questionnaire.exception.BusinessException;
import com.online.questionnaire.model.User;
import com.online.questionnaire.service.UserService;
import com.online.questionnaire.mapper.UserMapper;
import com.online.questionnaire.utils.PwdUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;

import static com.online.questionnaire.constant.UserConstant.BAN_ROLE;
import static com.online.questionnaire.constant.UserConstant.USER_LOGIN_STATE;

/**
* @author 27701
* @description 针对表【user(用户)】的数据库操作Service实现
* @createDate 2024-04-27 14:23:44
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    //默认头像
    public final static String defAvatar = "https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png";
    @Resource
    private UserMapper userMapper;

    @Override
    public int register(User user) {
        var userQuery = new LambdaQueryWrapper<User>();
        userQuery.eq(User::getUserAccount, user.getUserAccount());
        if (userMapper.exists(userQuery)){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"帐号已存在");
        }
        user.setUserName(user.getUserAccount());
        user.setUserAvatar(defAvatar);
        user.setUserPassword(PwdUtil.toMD5_SALT(user.getUserPassword()));
        return userMapper.insert(user);
    }


    @Override
    public User login(User user) {
        var userQuery = new LambdaQueryWrapper<User>();
        userQuery.eq(User::getUserAccount, user.getUserAccount());
        userQuery.eq(User::getUserPassword, PwdUtil.toMD5_SALT(user.getUserPassword()));
        User loginUser = userMapper.selectOne(userQuery);
        if (loginUser == null){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"帐号或密码错误");
        }
        return loginUser;
    }

    @Override
    public boolean userIsLogin(HttpServletRequest request) {
        return request.getSession().getAttribute(USER_LOGIN_STATE) != null;
    }

    @Override
    public User getLoginUser(HttpServletRequest request) {
        if (request == null){
            return null;
        }
        Object attribute = request.getSession().getAttribute(USER_LOGIN_STATE);
        if (attribute == null){
            return null;
        }
        Long uId = Long.parseLong(attribute.toString());
        User byId = this.getById(uId);
        if (byId != null ){
            if (BAN_ROLE.equals(byId.getUserRole())){
                throw new BusinessException(ErrorCode.OPERATION_ERROR,"用户" + byId.getUserAccount() + "被封禁");
            }
            return byId;
        }else{
            return null;
        }
    }

    @Override
    public String getUserNameById(Long userId) {
        User user = userMapper.selectById(userId);
        if (user == null){
            return "";
        }
        return user.getUserName();
    }


}




